package com.chuangyang.fixboxlib.utils;

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.chuangyang.fixboxlib.bean.LocationEntity;

public class LocationUtils {
	private static final String TAG = LocationUtils.class.getSimpleName();
	private final LocationMode mLocationMode = LocationMode.Hight_Accuracy;
	private final String mCoorType = "bd09ll";
	private final int mSpan = 10 * 1000; // refresh time interval
	private LocationClient mLocationClient;
	private Context mContext;
    private LocationListener locationListener;

    public static double latitude;
    public static double longitude;


    public interface  LocationListener{
        void onReceiveLocation(LocationEntity entity);
    }

	public LocationUtils(Context context) {
		this.mContext = context;
		initLocation();
	}


	private void initLocation() {
		mLocationClient = new LocationClient(mContext);
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(mLocationMode);
		option.setCoorType(mCoorType);
		option.setScanSpan(mSpan);
		option.setIsNeedAddress(true);
		mLocationClient.setLocOption(option);
	}

    private BDLocationListener bdLocationListener=new BDLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation bdlocation) {
            LocationEntity mLocationEntity=new LocationEntity();
            mLocationEntity.setAddress(bdlocation.getAddrStr());
            mLocationEntity.setLatitude(String.valueOf(bdlocation.getLatitude()));
            mLocationEntity.setLongitude(String.valueOf(bdlocation.getLongitude()));
            mLocationEntity.setFloor(bdlocation.getFloor());
            mLocationEntity.setDirection(String.valueOf(bdlocation.getDirection()));
            mLocationEntity.setDistrict(bdlocation.getDistrict());
            mLocationEntity.setLockType(bdlocation.getLocType());
            mLocationEntity.setOperators(bdlocation.getOperators());
            mLocationEntity.setProvince(bdlocation.getProvince());
            mLocationEntity.setStreet(bdlocation.getStreet());
            mLocationEntity.setStreetNumber(bdlocation.getStreetNumber());
            mLocationEntity.setGpstime(bdlocation.getTime());
            mLocationEntity.setLocaltime(System.currentTimeMillis()); // local  time
            mLocationEntity.setRadius(String.valueOf(bdlocation.getRadius()));
            LogUtils.LOGI(TAG, mLocationEntity.toString());
            locationListener.onReceiveLocation(mLocationEntity);
            latitude=bdlocation.getLatitude();
            longitude=bdlocation.getLongitude();
        }
    };

    public void startLocation(LocationListener listener){
        locationListener=listener;
        mLocationClient.registerLocationListener(bdLocationListener);
        mLocationClient.start();
        LogUtils.LOGI(TAG, "start location...");
    }


	public boolean isStarted() {
		return mLocationClient.isStarted();
	}

	public void stopLocation() {
		mLocationClient.stop();
		LogUtils.LOGI(TAG, "stop location...");
	}
}
